import RankingAPI from '../../common/api/biosafety/ranking.api.js';
import doT from '../../../vendor/template/doT.js';
import { DictionaryType, requestDict } from "../../common/dictionary/dictionary-type.js";
import { uploadFile, requestFilePath, publicFileDraw, sliceFileName } from "../../common/api/file-request.js";

(function (window, document, $) {
    let _vm, timeIsShow = false, pigFarmIsShow = false, _addressData = {}, list = [], time_interval = [], pigType = 0
    const datesTemplate = doT.template($('#dates-template').text());
    const listTemplate = doT.template($('#list-template').text());
    const _json = Utils.ParsingUrl(window.location);
    $(function () {
        $('body').attr('data-weui-theme', 'light'); //强制光明模式

        $(document).on("click", '.dropdown_item', function () {
            $(this).addClass('active').siblings().removeClass('active')
            $('.rankingContent').empty();
            // 关闭
            $('.dropdown').css('display', 'none')
            $('#times i').css('rotate', "0deg")
            // 控制显示和隐藏
            timeIsShow = false
            // 点击时间列表
            if ($(this).data("type") == 0) {
                getTimes($(this).data("val"))
            }
        })

        _initCommand();
        _initClick();
    });

    //命令初始化
    const _initCommand = async () => {
        //审查地点查询
        let res = await requestDict(DictionaryType.AUDITADDRESS_TYPE);
        res.data.unshift({ displayName: "全部", value: 0 })
        res.data.forEach(item => {
            _addressData[item.value] = item.displayName
        });
        // 获取时间
        time_interval = getHalfYear();
        $('.times').empty().append(datesTemplate(time_interval));
        if (Utils.IsNotNull(_json.bizdate)) {
            $('#queryTime').text(_json.bizdate)
            getTimes(time_interval.find(s => s.text == _json.bizdate).val)
        } else {
            $('#queryTime').text(time_interval[0].text)
            getTimes(time_interval[0].val)
        }
    }

    // 获取数据
    const getTimes = (data) => {
        RankingAPI.postAuditformBillsFail({
            "period": data
        }).then(res => {
            list = res;
            list.forEach(item => { item.text = _addressData[item.type] })
            $('.rankingContent').empty().append(listTemplate(list));
        })
        $('#queryTime').text((time_interval.find(item => item.val == data)).text)
    }

    // 获取时间间隔
    const getHalfYear = () => {
        // 获取当前时间
        let data = new Date();
        // 获取年份
        let year = data.getFullYear();
        // 获取月份
        let mon = data.getMonth() + 2;
        let array = new Array();
        for (let i = 0; i < 6; i++) {
            mon = mon - 1;
            if (mon <= 0) {
                year = year - 1;
                mon = mon + 12;
            }
            if (mon < 10) {
                mon = "0" + mon;
            }
            array[i] = { val: `${year}${mon}`, text: `${year}年${mon}月` };
        }
        return array;
    }

    const _initClick = () => {
        // 时间
        $('#times').on("click", function () {
            // 旋转图标
            $(this).find('i').css('rotate', timeIsShow ? "0deg" : "90deg")
            // 显示 下拉框
            $(".times").css("display", timeIsShow ? 'none' : 'block')
            // 点击改变状态
            timeIsShow = !timeIsShow;
        })
    }

})(window, document, jQuery)